package com.myidis.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myidis.entity.Department;
import com.myidis.response.DepartmentRes;
import com.myidis.servlet.DeptServlet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/dept")
@Api(tags = "部门信息接口")
public class DeptController {
	@Autowired
	private DeptServlet deptServlet;

	@ResponseBody
	@ApiOperation("部门列表获取，按树的方式")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public List<DepartmentRes> deptList() {
		List<DepartmentRes> res = new ArrayList<>();
		res.add(deptServlet.all());
		return res;
	}
	
	@ResponseBody
	@ApiOperation("获取所有部门的信息")
	@RequestMapping(value="/all",method=RequestMethod.POST)
	public List<Department> allDept() {
		return deptServlet.getAll();
	}
	
	@ResponseBody
	@ApiOperation("获取单个部门的信息")
	@RequestMapping(value="/entity",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public Department one(@RequestBody Department dept) {
		return deptServlet.getOne(dept.getId());
	}
	
	@ResponseBody
	@ApiOperation("添加一个部门")
	@RequestMapping(value="/add",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public int add(@RequestBody Department dept) {
		return deptServlet.insert(dept);
	}
	
	@ResponseBody
	@ApiOperation("添加一组部门")
	@RequestMapping(value="/addAll",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public int addAll(@RequestBody List<Department> depts) {
		return deptServlet.insertAll(depts);
	}
	
	@ResponseBody
	@ApiOperation("修改一个部门")
	@RequestMapping(value="/upd",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
	public int upd(@RequestBody Department dept) {
		return deptServlet.update(dept);
	}

	@ResponseBody
	@ApiOperation("删除一个部门")
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public int del(@RequestBody int id) {
		return deptServlet.delete(id);
	}
	
	@ResponseBody
	@ApiOperation("删除一组部门")
	@RequestMapping(value="/delAll",method=RequestMethod.POST)
	public int delAll(@RequestBody int[] ids) {
		return deptServlet.deleteAll(ids);
	}
}
